
package com.j2eefast.framework.code.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.framework.code.entity.SerialEntity;
import com.j2eefast.framework.code.mapper.SerialMapper;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>序列号管理</p>
 *
 * @author: summer
 * @date: 2021-05-29 16:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class SerialService extends ServiceImpl<SerialMapper, SerialEntity> {

	@Resource
	public SerialMapper serialMapper;


	/**
	 * 页面展示查询翻页
	 */
	public PageUtil findPage(Map<String, Object> params) {
		String codeType = (String) params.get("codeType");
		String name = (String) params.get("name");
		String codeRule = (String) params.get("codeRule");
		String remark = (String) params.get("remark");


//
//		Page<PmsSerialEntity> page = pmsSerialMapper.findPage(
//				new Query<PmsProjectEntity>(params).getPage(),
//				StrUtil.nullToDefault(codeType,""),
//				StrUtil.nullToDefault(name,""),
//				StrUtil.nullToDefault(codeRule,""),
//				StrUtil.nullToDefault(remark,""),

		Page<SerialEntity> page = serialMapper.findPage(
				new Query<SerialEntity>(params).getPage(),
//				StrUtil.nullToDefault(sampleCode,""),
//				StrUtil.nullToDefault(sampleType,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}

	@Transactional(rollbackFor = Exception.class)
	public boolean deleteByIds(Long[] ids) {

		return serialMapper.deleteByIds(ids);

	}
	@Transactional(rollbackFor = Exception.class)
	public boolean updateCheck(Long id) {
		Long userId= UserUtils.getUserInfo().getId();

		return serialMapper.updateCheck(id ,userId);

	}

	/**
	 * 定义序号
	 * */

	public Map<String,String> formatTitle(Long total, String code){
		Map<String, String> map = new HashMap<String,String>();       //数据采用的哈希表结构

		//查询工程编号
//		PmsParticipantEntity pmsParticipantEntity = pmsParticipantMapper.selectParticipant();
		//{项目名称}项目报备申请-{创建人}（{创建时间}）
		SerialEntity pmsSerialEntity=serialMapper.selectSerial(code);
		String title=pmsSerialEntity.getCodeRule();
		Date date=new Date();
//		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHss");
		//String date = sdf.format(new Date());
		String dateString = formatter.format(date);
		title =title.replace("{YMDHS}", dateString);

		SimpleDateFormat formatters = new SimpleDateFormat("yyyyMMdd");
		String dateStrings = formatters.format(date);
		title = title.replace("{YMD}", dateStrings);
		String total1=Long.toString(total+1L);
//		if(total!=null){
//			total1 = Long.toString(total+1L);
//		}
		title = title.replace("{序号}", total1);
		map.put("title",title);
		map.put("total",total1);
		return map;
	}

	/**
	 * 默认编号
	 * */
	public Map<String,String> formatTitle(Long total){
		Map<String, String> map = new HashMap<String,String>();       //数据采用的哈希表结构
//		PmsSerialEntity pmsSerialEntity=pmsSerialMapper.selectSerial(id);
//		String title=pmsSerialEntity.getCodeRule();
		String title="{YMDHS}-{序号}";
		Date date=new Date();
//		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHss");
		//String date = sdf.format(new Date());
		String dateString = formatter.format(date);
		title =title.replace("{YMDHS}", dateString);

		SimpleDateFormat formatters = new SimpleDateFormat("yyyyMMdd");
		String dateStrings = formatters.format(date);
		title = title.replace("{YMD}", dateStrings);
		String total1="1";
		if(total!=null){
			total1 = Long.toString(total+1L);
		}
		title = title.replace("{序号}", total1);
		map.put("title",title);
		map.put("total",total1);
		return map;
	}

}
